package com.cg.offer;

import org.junit.Test;

import java.util.ArrayDeque;
import java.util.Deque;

/**
 * 剑指Offer 09.用两个栈实现队列
 *
 * @program: LeetCode->Offer_09
 * @description: 剑指Offer 09.用两个栈实现队列
 * @author: cg
 * @create: 2022-03-09 20:42
 **/
public class Offer_09 {
    @Test
    public void test09() {
        CQueue queue = new CQueue();
        System.out.println(queue.deleteHead());
        queue.appendTail(5);
        queue.appendTail(2);
        System.out.println(queue.deleteHead());
        System.out.println(queue.deleteHead());
    }

}

/**
 * 用两个栈实现一个队列。队列的声明如下，请实现它的两个函数 appendTail 和 deleteHead ，分别完成在队列尾部插入整数和在队列头部删除整数的功能。(若队列中没有元素，deleteHead 操作返回 -1 )
 * <p>
 * 示例 1：
 * 输入：
 * ["CQueue","appendTail","deleteHead","deleteHead"]
 * [[],[3],[],[]]
 * 输出：[null,null,3,-1]
 * <p>
 * 示例 2：
 * 输入：
 * ["CQueue","deleteHead","appendTail","appendTail","deleteHead","deleteHead"]
 * [[],[],[5],[2],[],[]]
 * 输出：[null,-1,null,null,5,2]
 * <p>
 * 提示：
 * 1 <= values <= 10000
 * 最多会对 appendTail、deleteHead 进行 10000 次调用
 */
class CQueue {
    Deque<Integer> deque1;
    Deque<Integer> deque2;

    public CQueue() {
        deque1 = new ArrayDeque<Integer>();
        deque2 = new ArrayDeque<Integer>();
    }

    public void appendTail(int value) {
        while (!deque2.isEmpty()) {
            deque1.push(deque2.poll());
        }
        deque1.push(value);
    }

    public int deleteHead() {
        if (deque1.isEmpty() && deque2.isEmpty()) {
            return -1;
        }
        while (!deque1.isEmpty()) {
            deque2.push(deque1.poll());
        }
        return deque2.pop();
    }
}